<?php

function get_reset_password_email_body($name, $username, $link) {
    return sprintf(elgg_echo('reset_password:email:body'), $name, $link);
}

function get_reset_password_email_subject($name) {
    return sprintf(elgg_echo('reset_password:email:subject'), $name);
}

function get_change_password_email_body($name, $username, $password) {
    return sprintf(elgg_echo('change_password:email:body'), $name, $password);
}

function get_change_password_email_subject($name) {
    return sprintf(elgg_echo('change_password:email:subject'), $name);
}

function get_reset_password_email_body_confirm($name, $username, $password) {
    return
    sprintf(elgg_echo('confirm_reset_password:email:body'), $name, $password
    );
}

function get_reset_password_email_subject_confirm($name) {
    return sprintf(elgg_echo('confirm_reset_password:email:subject'), $name);
}

function greengar_change_password($userid, $password) {

    $user = get_user($userid);

    global $CONFIG;

    $salt = genRandomString(8);
    $user->salt = $salt; // Reset the salt
    $user->password = generate_user_password($user, $password);
    $user->setMetaData('isWPUser', 0);
    $user->save();

    $subject = get_change_password_email_subject($user->name);
    $body = get_change_password_email_body($user->name, $user->username, $password);
    $result = notify_user($user->guid, $CONFIG->site->guid, $subject, $body, NULL, 'email');
}

function greengar_reset_password($userid) {

    $user = get_user($userid);
    login($user);
    global $CONFIG;

    $code = urlencode(compute_hash($userid . ',' . $user->email . ',' . $user->salt, RESET_PASSWORD));
    $user->clearMetaData('reset_password');

    $user->setMetaData('reset_password', urldecode($code));
    $user->save();
    logout();
    //echo urldecode($code) . '<br>';
    //echo $user->getMetaData('reset_password') . '<br>';
    $link = "{$CONFIG->site->url}pg/greengar/user/confirm_reset_password?user_id=$userid&code=$code";
    $subject = get_reset_password_email_subject($user->name);
    $body = get_reset_password_email_body($user->name, $user->username, $link);
    greengar_send_mail($user->email, $subject, $body);
    //$result = notify_user($user->guid, $CONFIG->site->guid, $subject, $body, NULL, 'email');
}

function greengar_confirm_reset_password($userid, $code) {

    $user = get_user($userid);
    
    global $CONFIG;
//    echo $code . '<br>';
//    echo $user->getMetaData('reset_password') . '<br>';
    
    $check = ($code == $user->getMetaData('reset_password'));
//    echo $code . ' vs ' . compute_hash($userid . ',' . $user->email, RESET_PASSWORD) . '<br>';
    if ($check) {
        $password = genRandomString(8);
//        $password = '12345678';
        $salt = genRandomString(8);
//        $salt = '12345678';
		//echo $password . '-' . $salt;
        login($user);
        $user->clearMetaData('reset_password');
        $user->salt = $salt; // Reset the salt
        $user->password = generate_user_password($user, $password);
        $user->setMetaData('isWPUser', 0);
        $user->save();
        $subject = get_reset_password_email_subject_confirm($user->name);
        $body = get_reset_password_email_body_confirm($user->name, $user->username, $password);
		//echo '<br>';
		//echo $subject;
		//echo '<br>';
		//echo $body;
		greengar_send_mail($user->email, $subject, $body);
        logout();
        
        //$result = notify_user($user->guid, $CONFIG->site->guid, $subject, $body, NULL, 'email');
        return true;
    } else {
        return false;
    }
}

?>